#!/usr/bin/env python
# -*- coding:utf-8 -*-

# file:conftest.py.py
# author:Nathan
# datetime:2022/3/15 17:53
# software: PyCharm

"""
    存放fixture固件的配置文件
"""

import os

import pytest
import requests

from app.unit_test.utils.read_env import read_env,write_env


@pytest.fixture(scope="function")
def env_dict():
    """获得环境变量字典"""
    env_path = os.path.join(os.path.dirname(__file__), '.env')
    env_dict = read_env(env_path)
    return env_dict

@pytest.fixture(scope="module")
def get_and_write_token():
    """获得登录前置"""
    # 创建一个session对象，用于维持登录状态
    session = requests.Session()

    # 在session对象中进行登录操作
    login_url = "http://127.0.0.1:5000/api_1_0/user/login"
    login_data = {
        "account": "13966666666",
        "password": "112233445566"
    }
    response = session.post(login_url, data=login_data)
    res_data = response.json()

    login_token = res_data['data'].get('Token')
    env_path = os.path.join(os.path.dirname(__file__), '.env')

    write_env(env_path, login_token)

